package com.gwsdk.paysdk.floatview;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.animation.FastOutLinearInInterpolator;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import com.gwsdk.open.GWApi;
import com.gwsdk.paysdk.acitvityV2.GWMainMenuActivity;
import com.gwsdk.paysdk.bean.GWRoomInfoModel;
import com.gwsdk.paysdk.bean.GWRoomSeatModel;
import com.gwsdk.paysdk.bean.SwitchManager;
import com.gwsdk.paysdk.common.Constant;
import com.gwsdk.paysdk.dialog.GWVoiceTipDialog;

import com.gwsdk.paysdk.utils.AppStatusManager;
import com.gwsdk.paysdk.utils.AppUtils;
import com.gwsdk.paysdk.utils.GWInflaterUtils;
import com.gwsdk.paysdk.utils.SharedPreferencesUtils;
import com.gwsdk.paysdk.view.round.CircleImageView;

import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;


public class GWFloatView extends FrameLayout implements
        View.OnTouchListener{
    private String TAG = "GWFloatView";

    private WindowManager.LayoutParams wmParams;
    private WindowManager wm;
    private Context mContext;
    // 悬浮栏位置
    private final static int LEFT = 0;
    private final static int RIGHT = 1;
    private final static int TOP = 3;
    private final static int BUTTOM = 4;
    private int screenWidth,screenHeight,mFloatX = 10,mFloatY = 100;
    private ImageView closeBtn;
    private CircleImageView iconImage;
    private View voice_view;
    private LinearLayout menu_view;
    private View float_view;

    private boolean isShowMenuView = false;

    private int speed = 10;//移动速度
    private int timeValue = 0; //移动计步
    private volatile boolean isRunSide = false;//（开始/停止）平移悬浮窗flag
    private volatile boolean isAutoHideRun = true;//（开始/停止）隐藏悬浮窗flag
    private Timer mTimer;//平移悬浮窗定时器
    private Timer autohideTimer = null;//隐藏悬浮窗定时器

    private volatile boolean hasEventDown;//判断是否有ACTION_DOWN事件触发的flag
    private float eventDownX, eventDownY;
    private float eventMoveX, eventMoveY;
    private float eventDownStartX;
    private float eventDownStartY;
    private float eventUpEndX;
    private float eventUpEndY;
    private volatile boolean isScroll;//判断悬浮窗口是否移动的flag
    private volatile boolean isLongClick;//判断触摸事件是否长按的flag
    private volatile AnimatorSet animatorSet = null;//组合动画

    private static GWFloatView instance = null;//单例模式
    private long lastClickTime = 0;
    private Handler handler = new Handler();
    private int clickNum = 0;
    private int teamCount = 0;
    private LinearLayout llSeatView;
    private HorizontalScrollView scrollView;
    private LinearLayout noJoinView;
    private TextView start_voice_app_btn;
    private TextView voice_tip,voice_online_member;

    public static GWFloatView getInstance(final Context context){
        if(instance == null) {
            synchronized (GWFloatView.class) {
                if(instance == null) {
                    instance = new GWFloatView(context);
                }
            }
        }
        return instance;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public GWFloatView(Context context) {
        super(context);
        mContext = context;
        wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        screenWidth = GWScreenUtils.getHasVirtualWidth(context);
        screenHeight = GWScreenUtils.getHasVirtualHight(context);
        wmParams = new WindowManager.LayoutParams();
        wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
        wmParams.format = PixelFormat.RGBA_8888; // 设置图片格式，效果为背景透明
        wmParams.gravity = 51;
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
                        | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        wmParams.width = LayoutParams.WRAP_CONTENT;
        wmParams.height = LayoutParams.WRAP_CONTENT;
        wmParams.x = mFloatX;
        wmParams.y = mFloatY;
        addView(createView(context));
        setOnTouchListener(this);
    }

    private View createView(final Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);

        View view = inflater.inflate(GWInflaterUtils.getLayout(context,"gw_float_menu_view"), null);
        closeBtn = view.findViewById(GWInflaterUtils.getControl(context,"close_voice_view_btn"));
        voice_view = view.findViewById(GWInflaterUtils.getControl(context,"voice_view"));
        menu_view = view.findViewById(GWInflaterUtils.getControl(context,"menu_view"));
        float_view = view.findViewById(GWInflaterUtils.getControl(context,"float_view"));
        iconImage = view.findViewById(GWInflaterUtils.getControl(context,"gw_icon"));
        llSeatView = view.findViewById(GWInflaterUtils.getControl(context,"ll_seat_view"));

        scrollView = view.findViewById(GWInflaterUtils.getControl(context,"scroll_view"));
        noJoinView = view.findViewById(GWInflaterUtils.getControl(context,"no_join_room"));
        start_voice_app_btn = view.findViewById(GWInflaterUtils.getControl(context,"start_voice_app_btn"));
        voice_tip = view.findViewById(GWInflaterUtils.getControl(context,"voice_tip"));
        voice_online_member = view.findViewById(GWInflaterUtils.getControl(context,"voice_online_member"));
        if(Constant.XFBitmap == null){
            String logo = SwitchManager.getInstance().floatingIconUrl;
            if (!TextUtils.isEmpty(logo)){
                Glide.with(context).load(logo).into(iconImage);
            }
        }else{
            iconImage.setImageBitmap(Constant.XFBitmap);
        }
        Random rand = new Random();
        int number = rand.nextInt(4789)+4000;
        String text = String.format("有%s人正在热聊该游戏",number);
        voice_online_member.setText(text);

        addListener();
        setSeatViews();
        return view;
    }

    private void addListener(){
        start_voice_app_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               openVoiceApp();
            }
        });
        closeBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                collapse();
            }
        });

        menu_view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                openMainMenuActivity();
            }
        });

        AppStatusManager.getInstance().setSeatListener(new AppStatusManager.GWRefreshRoomSeatListener() {
            @Override
            public void onRefreshSeat() {
                setSeatViews();
            }
        });
    }

    private void openVoiceApp(){
        if (AppUtils.checkAppInstalled(getContext(),"com.genwan.voice")){
            Bundle bundle = new Bundle();
            bundle.putInt("type",2);
            bundle.putString("className","com.gwsdk.paysdk.acitvityV2.GWAuthLoginActivity");
            AppUtils.jumpAPP(GWApi.getApi().getContext(), "com.genwan.voice",bundle);
        }else {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse(AppStatusManager.getInstance().downUrl);
            intent.setData(content_url);
            getContext().startActivity(intent);
        }
    }

    private void setSeatViews(){
        GWRoomInfoModel roomInfo = AppStatusManager.getInstance().roomInfo;
        if (roomInfo != null && roomInfo.room_id > 0){
            noJoinView.setVisibility(GONE);
            scrollView.setVisibility(VISIBLE);
            List<GWRoomSeatModel> allSeatList = AppStatusManager.getInstance().seatModels;
            llSeatView.removeAllViews();
            for (int i = 0; i < allSeatList.size(); i++) {
                if (allSeatList.get(i).isSeat){
                    RoomSeatView seatView = new RoomSeatView(mContext);
                    seatView.setSeatInfo(allSeatList.get(i));
                    llSeatView.addView(seatView);
                }
            }
            for (int i = 0; i < allSeatList.size(); i++) {
                if (!allSeatList.get(i).isSeat){
                    RoomSeatView seatView = new RoomSeatView(mContext);
                    seatView.setSeatInfo(allSeatList.get(i));
                    llSeatView.addView(seatView);
                }
            }
        }else {
            noJoinView.setVisibility(VISIBLE);
            scrollView.setVisibility(GONE);
        }

    }



    private void openMainMenuActivity(){
        Intent intent = new Intent(mContext, GWMainMenuActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

    public void show() {

        if(SupportAndroidVersion()) {
            if (isShown()) return;
            if(wm != null){
                wm.addView(this, wmParams);
            }
            //通过屏幕的宽度和高度的大小比较判别
            if(screenWidth < screenHeight) {//竖屏
                if (wmParams.x < screenWidth / 2 - getMeasuredWidth() / 2) {
                    autoHideViewToSide(LEFT, 3000);
                } else {
                    autoHideViewToSide(RIGHT, 3000);
                }
            }else {//横屏
                if (wmParams.x <= screenWidth / 4 || (wmParams.y >= screenHeight / 2 && wmParams.x <= screenWidth / 2)) {
                    autoHideViewToSide(LEFT, 3000);
                } else if (wmParams.x >= screenWidth * 3 / 4 || (wmParams.y >= screenHeight / 2 && wmParams.x > screenWidth / 2)) {
                    autoHideViewToSide(RIGHT, 3000);
                } else {
                    autoHideViewToSide(TOP, 3000);
                }
            }
        }
    }


    public void expand() {
        if (isShowMenuView) return;
        isShowMenuView = true;
        voice_view.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final int viewWidth = voice_view.getMeasuredWidth();
        voice_view.getLayoutParams().width = 0;
        voice_view.setVisibility(View.VISIBLE);

        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if (interpolatedTime == 1) {
                    voice_view.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
                    menu_view.setVisibility(VISIBLE);
                } else {
                    voice_view.getLayoutParams().width = (int) (viewWidth * interpolatedTime);
                }
                voice_view.requestLayout();
            }
        };
        animation.setDuration(300);
        animation.setInterpolator(new FastOutLinearInInterpolator());
        voice_view.startAnimation(animation);

        if (!SharedPreferencesUtils.getIsFirstOpen(mContext)){
            GWVoiceTipDialog dialog = new GWVoiceTipDialog(mContext);

            FragmentManager fm = ((Activity)mContext).getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(dialog, TAG);
            ft.show(dialog);
            ft.commitAllowingStateLoss();
        }
        SharedPreferencesUtils.setIsFirstOpen(mContext,true);
        AppStatusManager.getInstance().getRoomInfo();
    }

    public void collapse() {
        if (!isShowMenuView) return;
        isShowMenuView = false;
        voice_view.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        menu_view.setVisibility(GONE);
        final int viewWidth = voice_view.getMeasuredWidth();
        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if (interpolatedTime == 1) {
                    voice_view.setVisibility(View.GONE);
                    autoViewRunToSide(float_view,5000);
                } else {
                    voice_view.getLayoutParams().width = viewWidth - (int) (viewWidth * interpolatedTime);
                    voice_view.requestLayout();
                }
            }
        };
        animation.setDuration(300);
        animation.setInterpolator(new FastOutLinearInInterpolator());
        voice_view.startAnimation(animation);
    }

    /**关闭悬浮窗口 */
    public void close(){
        destory();
    }

    /**销毁悬浮窗 */
    private void destory() {
        AppStatusManager.getInstance().setSeatListener(null);
        if(isShown()) {
            if (wm != null)
                wm.removeViewImmediate(this);
        }
        wm = null;
        instance = null;
    }

    /**自动隐藏悬浮窗 */
    private void autoHideViewToSide(final int side,int time){
        TimerTask autohideTimerTask = new TimerTask() {
            @Override
            public void run() {
                if(!isAutoHideRun) {
                    if(autohideTimer != null){
                        autohideTimer.cancel();
                        autohideTimer = null;
                    }
                    this.cancel();
                    return;
                }
                ((Activity)mContext).runOnUiThread(new Runnable() {
                    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
                    @Override
                    public void run() {
                        if(!isAutoHideRun)
                            return;
                        collapse();
                        float_view.setAlpha(0.7f);
                        if(animatorSet != null && (animatorSet.isStarted() || animatorSet.isRunning())) {
                            animatorSet.end();
                            animatorSet.cancel();
                            animatorSet = null;
                        }
                        ObjectAnimator animatorTranslation = null;
                        ObjectAnimator animatorRotation = null;
                        switch (side){
                            case LEFT:
                                animatorTranslation = new ObjectAnimator().ofFloat(float_view,"translationX",0,-float_view.getMeasuredWidth()*1/3);
                                animatorRotation = new ObjectAnimator().ofFloat(float_view,"rotation",0,-360);
                                break;
                            case RIGHT:
                                animatorTranslation = new ObjectAnimator().ofFloat(float_view,"translationX",0,float_view.getMeasuredWidth()*1/3);
                                animatorRotation = new ObjectAnimator().ofFloat(float_view,"rotation",0,360);
                                break;
                            case TOP:
                                animatorTranslation = new ObjectAnimator().ofFloat(float_view,"translationY",0,-float_view.getMeasuredWidth()*1/3);
                                animatorRotation = new ObjectAnimator().ofFloat(float_view,"rotation",0,-360);
                                break;
                            case BUTTOM:
                                break;
                        }
                        animatorSet = new AnimatorSet();  //组合动画
                        animatorSet.playTogether(animatorTranslation,animatorRotation); //设置动画
                        animatorSet.setDuration(1000);  //设置动画时间
                        animatorSet.start(); //启动
                    }
                });
            }
        };
        isAutoHideRun = true;
        autohideTimer = new Timer();
        autohideTimer.schedule(autohideTimerTask,time);//隐藏浮标：延迟时间为time（秒）
    }

    /**判断当前设备android版本是否符合要求 */
    public boolean SupportAndroidVersion() {
        int curApiVersion = Build.VERSION.SDK_INT;
        if (curApiVersion >= 14) {
            return true;
        }
        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // 获取相对屏幕的坐标， 以屏幕左上角为原点
        int[] location = new  int[2] ;
        v.getLocationInWindow(location);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                hasEventDown = true;
                /*
                 * 触发ACTION_DOWN把所有的作用及效果全部取消并复原
                 */
                if(mTimer != null){
                    isRunSide = false;
                    mTimer.cancel();
                    mTimer = null;
                }
                if(autohideTimer != null){
                    isAutoHideRun = false;
                    autohideTimer.cancel();
                    autohideTimer = null;
                }
                eventDownX = event.getX();
                eventDownY = event.getY();
                eventDownStartX = event.getRawX();
                eventDownStartY = event.getRawY();

                if(animatorSet != null && (animatorSet.isStarted() || animatorSet.isRunning())) {
                    animatorSet.end();
                    animatorSet.cancel();
                    animatorSet = null;
                }
                float_view.setAlpha(1.0f);

                ObjectAnimator translationAnimator = null;
                //得到view在屏幕中的位置
                v.getLocationOnScreen(location);
                //判断位于屏幕的位置
                if(screenWidth < screenHeight) {//竖屏
                    if (location[0] < screenWidth / 2 - getMeasuredWidth() / 2) {
                        translationAnimator = new ObjectAnimator().ofFloat(float_view,"translationX",-v.getMeasuredWidth()*1/2,0);//左侧
                    } else {
                        translationAnimator = new ObjectAnimator().ofFloat(float_view,"translationX",v.getMeasuredWidth()*1/2,0);//右侧
                    }
                }else {//横屏
                    if (location[0] <= screenWidth / 4 || (location[1] >= screenHeight / 2 && location[0] <= screenWidth / 2)) {
                        translationAnimator = new ObjectAnimator().ofFloat(float_view,"translationX",-v.getMeasuredWidth()*1/2,0);//左侧
                    } else if (location[0] >= screenWidth * 3 / 4 || (location[1] >= screenHeight / 2 && location[0] > screenWidth / 2)) {
                        translationAnimator = new ObjectAnimator().ofFloat(float_view,"translationX",v.getMeasuredWidth()*1/2,0);//右侧
                    } else {
                        translationAnimator = new ObjectAnimator().ofFloat(float_view,"translationY",-v.getMeasuredWidth()*1/2,0);//上侧
                    }
                }
                animatorSet = new AnimatorSet();  //组合动画
                animatorSet.playTogether(translationAnimator); //设置动画
                animatorSet.setDuration(0);  //设置动画时间
                animatorSet.start(); //启动
                break;
            case MotionEvent.ACTION_MOVE:
                if(!hasEventDown)
                    return false;
                eventMoveX = event.getRawX();
                eventMoveY = event.getRawY();
                if(!isLongClick) {
                    isLongClick = isLongPressed(event.getDownTime(), event.getEventTime(), 1000);
                }
                if(isScroll || isLongClick || (Math.abs(eventMoveX - eventDownStartX) + Math.abs(eventMoveY - eventDownStartY)) >= (v.getMeasuredWidth()+v.getMeasuredHeight())/3) {
                    isScroll = true;
                    updateViewPosition();
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if(!hasEventDown)
                    return false;
                eventUpEndX = event.getRawX();
                eventUpEndY = event.getRawY();
                v.getLocationOnScreen(location);
                if(!isScroll && ((Math.abs(eventUpEndX - eventDownStartX) + Math.abs(eventUpEndY - eventDownStartY)) < (v.getMeasuredWidth()+v.getMeasuredHeight())/3)){
                    v.getLocationOnScreen(location);
                    clickNum++;
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (clickNum == 1) {
                                if (isShowMenuView){
                                    collapse();
                                    autoViewRunToSide(v,5000);
                                }else {
                                    expand();
                                }
                            }else if(clickNum==2){
                                openMainMenuActivity();
                            }
                            //防止handler引起的内存泄漏
                            handler.removeCallbacksAndMessages(null);
                            clickNum = 0;
                        }
                    },200);

                }else{
                    autoViewRunToSide(v,3000);
                }
                isScroll = false;
                isLongClick = false;
                hasEventDown = false;
                break;
        }
        return false;
    }

    /**
     * 自动移动位置
     */
    private int[] location = new int[2];//悬浮窗口在屏幕的x、y位置
    private void autoViewRunToSide(View v, int autohideTime) {
        if(v == null)
            return;
        if (isShowMenuView) return;
        //得到view在屏幕中的位置
        v.getLocationOnScreen(location);
        //判断位于屏幕的位置
        if(screenWidth < screenHeight) {//竖屏
            if (location[0] < screenWidth / 2 - v.getMeasuredWidth() / 2) {
                RunToSide(LEFT,autohideTime);//左侧
            } else {
                RunToSide(RIGHT,autohideTime);//右侧
            }
        }else {//横屏
            if (location[0] <= screenWidth / 4 || (location[1] >= screenHeight / 2 && location[0] <= screenWidth / 2)) {//左侧
                RunToSide(LEFT, autohideTime);
            } else if (location[0] >= screenWidth * 3 / 4 || (location[1] >= screenHeight / 2 && location[0] > screenWidth / 2)) {//右侧
                RunToSide(RIGHT, autohideTime);
            } else {//上侧
                RunToSide(TOP, autohideTime);
            }
        }
    }

    public void RunToSide(final int side,final int autohideTime){
        if(side != TOP && screenWidth > screenHeight)//横屏且非上侧
            speed = 15;
        else
            speed = 10;
        int runLen = 0;
        switch (side){
            case LEFT:
                runLen = location[0];
                break;
            case RIGHT:
                runLen = screenWidth - location[0];
                break;
            case TOP:
                runLen = location[1];
                break;
            case BUTTOM:
                break;
        }
        timeValue = runLen/speed;

        final TimerTask task = new TimerTask() {
            @Override
            public void run() {
                if(!isRunSide) {
                    if(mTimer != null){
                        mTimer.cancel();
                        mTimer = null;
                    }
                    this.cancel();
                    return;
                }
                timeValue--;
                if (timeValue >= 0) {
                    switch (side){
                        case LEFT:
                            wmParams.x = timeValue * speed;
                            break;
                        case RIGHT:
                            wmParams.x = screenWidth - timeValue*speed;
                            break;
                        case TOP:
                            wmParams.y = timeValue * speed;
                            break;
                        case BUTTOM:
                            break;
                    }
                    ((Activity)mContext).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(!isRunSide)
                                return;

                            if(wm==null){
                                return;
                            }
                            wm.updateViewLayout(GWFloatView.this,wmParams);
                        }
                    });
                }else{
                    isRunSide = false;
                    if(mTimer!=null) {
                        mTimer.cancel();
                    }
                    switch (side){
                        case LEFT:
                            wmParams.x = 0;
                            break;
                        case RIGHT:
                            wmParams.x = screenWidth - getMeasuredWidth();
                            break;
                        case TOP:
                            wmParams.y = 0;
                            break;
                        case BUTTOM:
                            break;
                    }
                    ((Activity)mContext).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(wm==null){
                                return;
                            }
                            wm.updateViewLayout(GWFloatView.this,wmParams);
                            SharedPreferences sharedPreferences = mContext.getSharedPreferences("location", Context.MODE_PRIVATE);
                            SharedPreferences.Editor edit = sharedPreferences.edit();
                            edit.putString("float_location", wmParams.x + "|" + wmParams.y);
                            edit.commit();
                        }
                    });
                    TimerTask autohideTimerTask = new TimerTask() {
                        @Override
                        public void run() {
                            if(!isAutoHideRun) {
                                if(autohideTimer != null){
                                    autohideTimer.cancel();
                                    autohideTimer = null;
                                }
                                this.cancel();
                                return;
                            }
                            ((Activity)mContext).runOnUiThread(new Runnable() {
                                @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
                                @Override
                                public void run() {
                                    if(!isAutoHideRun)
                                        return;
                                    collapse();
                                    float_view.setAlpha(0.7f);
                                    if(animatorSet != null && (animatorSet.isStarted() || animatorSet.isRunning())) {
                                        animatorSet.end();
                                        animatorSet.cancel();
                                    }
                                    ObjectAnimator animatorTranslation = null;
                                    ObjectAnimator animatorRotation = null;
                                    switch (side){
                                        case LEFT:
                                            animatorTranslation = new ObjectAnimator().ofFloat(float_view,"translationX",0,-float_view.getMeasuredWidth()*1/2);
                                            animatorRotation = new ObjectAnimator().ofFloat(float_view,"rotation",0,-360);
                                            break;
                                        case RIGHT:
                                            animatorTranslation = new ObjectAnimator().ofFloat(float_view,"translationX",0,float_view.getMeasuredWidth()*1/2);
                                            animatorRotation = new ObjectAnimator().ofFloat(float_view,"rotation",0,360);
                                            break;
                                        case TOP:
                                            animatorTranslation = new ObjectAnimator().ofFloat(float_view,"translationY",0,-float_view.getMeasuredWidth()*1/2);
                                            animatorRotation = new ObjectAnimator().ofFloat(float_view,"rotation",0,-360);
                                            break;
                                        case BUTTOM:
                                            break;
                                    }
                                    animatorSet = new AnimatorSet();  //组合动画
                                    animatorSet.playTogether(animatorTranslation,animatorRotation); //设置动画
                                    animatorSet.setDuration(1000);  //设置动画时间
                                    animatorSet.start(); //启动
                                }
                            });
                        }
                    };
                    isAutoHideRun = true;
                    autohideTimer = new Timer();
                    autohideTimer.schedule(autohideTimerTask,autohideTime);//隐藏浮标：延迟autohideTime（秒）
                }
                ((Activity)mContext).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        GWFloatView.this.invalidate();
                    }
                });
            }
        };
        isRunSide = true;
        mTimer = new Timer();
        mTimer.schedule(task, 0, 15); // 平移动画效果：隔15毫秒执行一次
    }

    /**
     * 更新悬浮窗窗口位置参数
     */
    private void updateViewPosition() {
        wmParams.x = (int) (eventMoveX - eventDownX );
        wmParams.y = (int) (eventMoveY - eventDownY );
        wm.updateViewLayout(this, wmParams);
    }

    /**
     * 判断是否有长按动作发生
     * @param lastDownTime 按下时间
     * @param thisEventTime 移动时间
     * @param longPressTime 判断长按时间的阀值
     */
    private boolean isLongPressed(long lastDownTime, long thisEventTime, long longPressTime){
        long intervalTime = thisEventTime - lastDownTime;
        if(intervalTime >= longPressTime){
            return true;
        }
        return false;
    }
}
